
#include "ui_page.h"

// GIF 显示模式:  0 - 按图片组预先加载至SDRAM, 避免与avi视频流竞争jpeg硬件解码器, 顶部字体滚动循环;         1 - 从 spi->sdram 加载图片，加载一张显示一张，顶部字体不滚动
#define GIF_SHOW_MODE 											( (LCD_HDOT >= 800 && LCD_VDOT >= 480/* && LCD_FMT == LCD_RGB888*/) ? 1 : 0) 

static lv_style_t Label_Style_Font;		 // Label 字体样式
static lv_obj_t *Label_Text_Core = NULL; // 中心展示语
static lv_obj_t *Label_Text_Top = NULL;	 // top 标语
static lv_obj_t *Btn_Left = NULL;		 // 视频按钮-左
static lv_obj_t *Btn_Right = NULL;		 // 视频按钮-右

static void label_set_font_style(lv_obj_t *label, lv_style_t *style_label);
static void pp_create_text_core(lv_obj_t *parent);
static void pp_delete_text_core(void);
static void pp_create_text_top(lv_obj_t *parent);
static void pp_delete_text_top(void);

/*为跳转pp页提前加载所需素材资源*/
bool load_pp_jump(void)
{
	if (0 != load_to_sdram_source(SPI, bin, File_Table.file[FLASH_FONT_SE].name, &Font_Addr)) //加载字库bin文件
		return false;
	
#if !GIF_SHOW_MODE
	for (uint32_t i = PP_GIF_START; i < PP_GIF_START + PP_GIF_SIZE; ++i)
	{
		if (0 != load_to_sdram_source(SPI, img, Image_Hash_Table[i].file.name, Image_Hash_Table[i].img))
			return false;
	}
#endif // GIF_SHOW_MODE
	
	return true;
}

/*退出pp页释放资源*/
void clean_pp_quit(void)
{
#if !GIF_SHOW_MODE
	for (uint32_t i = PP_GIF_START; i < PP_GIF_START + PP_GIF_SIZE; ++i)
	{
		if (Image_Hash_Table[i].img->data != NULL || Image_Hash_Table[i].img->data_size != 0)
		{
			sdram_free((void *)Image_Hash_Table[i].img->data);
			Image_Hash_Table[i].img->data = NULL;
			Image_Hash_Table[i].img->data_size = 0;
		}
	}
#endif // GIF_SHOW_MODE
	
	if (Font_Addr != 0)
	{
		sdram_free((void *)Font_Addr);
		Font_Addr = 0;
	}
}

/**
 * @brief	图片演示任务
 * @param 	task -任务对象
 * @retval	\
 */
static void gif_pp_task(lv_task_t *task)
{
	uint16_t index = GIF.index;
	index += PP_GIF_START;
	GIF.img = GIF_PP;
	GIF.index_max = PP_GIF_SIZE;

#if GIF_SHOW_MODE
	if (0 == load_to_sdram_source(SPI, img, Image_Hash_Table[index].file.name, Image_Hash_Table[index].img))
	{
#else
	{
#endif // GIF_SHOW_MODE
		lv_img_set_src(GIF.obj, &GIF.img[GIF.index++]); // 从 spi->sdram 加载图片，加载一张显示一张
		GIF.index = (GIF.index < GIF.index_max) ? GIF.index : 0;

#if GIF_SHOW_MODE
		LOG_INFO("[sdram_free] addr = 0x[%p]\r\n", Image_Hash_Table[index].img->data);
		sdram_free((void *)Image_Hash_Table[index].img->data);
#endif // GIF_SHOW_MODE
	}
	lv_obj_align(GIF.obj, NULL, LV_ALIGN_CENTER, 0, 0); //相对父级居中对齐
}

/**
 * @brief	跳转 Picture 页
 * @param 	parent 		- 页面父节点
 *          user_data 	- 用户自定义数据
 * @retval	obj 		- 页面光标
 */
lv_obj_t *page_jump_pp(lv_obj_t *parent, void *user_data)
{
	parent = Page_Table[*(uint16_t *)user_data].obj;
	GIF.index = 0;
	
	//加载所需素材
	if (load_pp_jump())
	{
		// font_setting
		pp_create_text_top(parent);
		pp_create_text_core(parent);
	}

	// video_btn_setting --- 仅创建一次,不删除一直保留
	if (NULL == Btn_Left && NULL == Btn_Right)
	{
		// btn_style_setting
		static lv_style_t style_videobtn;
		lv_style_init(&style_videobtn);
		lv_style_set_bg_opa(&style_videobtn, LV_STATE_DEFAULT, LV_OPA_TRANSP);		   //指定背景的不透明度
		lv_style_set_border_color(&style_videobtn, LV_STATE_DEFAULT, LV_COLOR_BLACK);  //指定边框的颜色 -黑色 LV_COLOR_WHITE / 白色 LV_COLOR_BLACK
		lv_style_set_border_opa(&style_videobtn, LV_STATE_DEFAULT, LV_OPA_TRANSP);	   //指定边框的不透明度
		lv_style_set_border_width(&style_videobtn, LV_STATE_DEFAULT, 0);			   //设置边框的宽度
		lv_style_set_outline_color(&style_videobtn, LV_STATE_DEFAULT, LV_COLOR_BLACK); //指定轮廓的颜色 -黑色 LV_COLOR_WHITE / 白色 LV_COLOR_BLACK
		lv_style_set_outline_opa(&style_videobtn, LV_STATE_DEFAULT, LV_OPA_TRANSP);	   //指定轮廓的不透明度
		lv_style_set_outline_width(&style_videobtn, LV_STATE_DEFAULT, 0);			   //设置轮廓的宽度
		lv_style_set_outline_pad(&style_videobtn, LV_STATE_DEFAULT, 0);				   //设置对象与轮廓之间的间距

		// Btn_Left_setting
		Btn_Left = lv_btn_create(parent, NULL);
		lv_obj_set_event_cb(Btn_Left, event_cb_video_btn_left);		   //设置回调
		lv_obj_add_style(Btn_Left, LV_BTN_PART_MAIN, &style_videobtn); //添加样式
		lv_obj_set_size(Btn_Left,
						(LV_HOR_RES_MAX - lv_obj_get_width(Label_Text_Core)) / 2,
						lv_obj_get_height(Label_Text_Core));	  //设置大小
		
		lv_obj_align(Btn_Left, NULL, LV_ALIGN_IN_LEFT_MID, 0, 0); //相对父级内部靠左居中对齐

		// Btn_Right_setting
		Btn_Right = lv_btn_create(parent, NULL);
		lv_obj_set_event_cb(Btn_Right, event_cb_video_btn_right);
		lv_obj_add_style(Btn_Right, LV_BTN_PART_MAIN, &style_videobtn);
		lv_obj_set_size(Btn_Right,
						(LV_HOR_RES_MAX - lv_obj_get_width(Label_Text_Core)) / 2,
						lv_obj_get_height(Label_Text_Core));		//设置大小
						
		lv_obj_align(Btn_Right, NULL, LV_ALIGN_IN_RIGHT_MID, 0, 0); //相对父级内部靠右居中对齐
	}
	// switch_ui_page
	lv_tabview_set_tab_act(TabView, *(uint16_t *)user_data, LV_ANIM_OFF);

	// video_setting
	PLAY_MV(GET_OFFSET_X(LV_HOR_RES_MAX, FLASH_VIDEO_AREA_W, 1), GET_OFFSET_Y(LV_VER_RES_MAX, FLASH_VIDEO_AREA_H, 1));
	return parent;
}

/**
 * @brief	开始/暂停 按钮
 * @param 	parent 		- 页面父节点
 *          user_data 	- 用户自定义数据
 * @retval	obj 		- 页面光标
 */
lv_obj_t *page_show_go(lv_obj_t *parent, void *user_data)
{
	if (NULL == GIF.task)
	{
		pp_delete_text_core();
		if (NULL == GIF.obj)
		{
			GIF.obj = lv_img_create(parent, NULL);
#if GIF_SHOW_MODE
			uint16_t index = GIF.index;
			index += PP_GIF_START;
			if (0 == load_to_sdram_source(SPI, img, Image_Hash_Table[index].file.name, Image_Hash_Table[index].img))
			{
				sdram_free((void *)Image_Hash_Table[index].img->data);
#else
			{
#endif // GIF_SHOW_MODE
				lv_img_set_src(GIF.obj, &GIF_PP[GIF.index++]);
				lv_obj_align(GIF.obj, NULL, LV_ALIGN_CENTER, 0, 0); //相对父级居中对齐
			}
		}
		GIF.task = lv_task_create(gif_pp_task, PP_TASK_MS, LV_TASK_PRIO_MID, user_data);
		lv_task_ready(GIF.task);
	}
	else
	{
		pp_create_text_core(parent);
		delete_gif(&GIF);
	}
	return Page_Obj_Cur;
}

/**
 * @brief	上一张 按钮
 * @param 	parent 		- 页面父节点
 *          user_data 	- 用户自定义数据
 * @retval	obj 		- 页面光标
 */
lv_obj_t *page_show_up(lv_obj_t *parent, void *user_data)
{
	if (NULL != GIF.task)
	{
		lv_task_del(GIF.task);
		GIF.task = NULL;
	}
	if (NULL == GIF.obj)
	{
		GIF.obj = lv_img_create(parent, NULL);
	}
	pp_delete_text_core();

	GIF.index = (GIF.index < sizeof(GIF_PP) / sizeof(GIF_PP[0])) ? GIF.index : 0; //先判断数据有效性

	if (GIF.index <= 1) //判断上一张是否到达边界
	{
		GIF.index = (GIF.index == 1) ? (sizeof(GIF_PP) / sizeof(GIF_PP[0]) - 1) : (sizeof(GIF_PP) / sizeof(GIF_PP[0]) - 2);
	}
	else
	{
		GIF.index -= 2;
	}

#if GIF_SHOW_MODE
	uint16_t index = GIF.index;
	index += PP_GIF_START;
	if (0 == load_to_sdram_source(SPI, img, Image_Hash_Table[index].file.name, Image_Hash_Table[index].img))
	{
		sdram_free((void *)Image_Hash_Table[index].img->data);
#else
	{
#endif // GIF_SHOW_MODE
		lv_img_set_src(GIF.obj, &GIF_PP[GIF.index++]);
		lv_obj_align(GIF.obj, NULL, LV_ALIGN_CENTER, 0, 0); //相对父级居中对齐
	}

	return Page_Obj_Cur;
}

/**
 * @brief	下一张 按钮
 * @param 	parent 		- 页面父节点
 *          user_data 	- 用户自定义数据
 * @retval	obj 		- 页面光标
 */
lv_obj_t *page_show_down(lv_obj_t *parent, void *user_data)
{
	if (NULL != GIF.task)
	{
		lv_task_del(GIF.task);
		GIF.task = NULL;
	}
	if (NULL == GIF.obj)
	{
		GIF.obj = lv_img_create(parent, NULL);
	}
	pp_delete_text_core();

	GIF.index = (GIF.index < sizeof(GIF_PP) / sizeof(GIF_PP[0])) ? GIF.index : 0;

#if GIF_SHOW_MODE
	uint16_t index = GIF.index;
	index += PP_GIF_START;
	if (0 == load_to_sdram_source(SPI, img, Image_Hash_Table[index].file.name, Image_Hash_Table[index].img))
	{
		sdram_free((void *)Image_Hash_Table[index].img->data);
#else
	{
#endif // GIF_SHOW_MODE
		lv_img_set_src(GIF.obj, &GIF_PP[GIF.index++]);
		lv_obj_align(GIF.obj, NULL, LV_ALIGN_CENTER, 0, 0); //相对父级居中对齐
	}
	return Page_Obj_Cur;
}

/*===========================本地函数定义=======================*/
/**
 * @brief	删除中心显示文本
 * @param 	\
 * @retval	\
 */
static void pp_delete_text_core(void)
{
	if (Label_Text_Core != NULL)
	{
		LOG_INFO(" delete [Label_Text_Core]\r\n");
		lv_obj_del(Label_Text_Core);
		Label_Text_Core = NULL;
	}
}

/**
 * @brief	创建中心显示文本
 * @param 	parent 		- 页面父节点
 * @retval	\
 */
static void pp_create_text_core(lv_obj_t *parent)
{
	pp_delete_text_core();
	LOG_INFO(" create [Label_Text_Core]\r\n");

	Label_Text_Core = lv_label_create(parent, NULL);
	label_set_font_style(Label_Text_Core, &Label_Style_Font);

	lv_label_set_text(Label_Text_Core, "#ff0000 3.3V供电系列:# SWM181 SWM190 SWM320 SWM341 \n");
	lv_label_ins_text(Label_Text_Core, LV_LABEL_POS_LAST, "#ff0000 5V供电系列:# SWM201 SWM260 \n"); //在指定位置追加文本
	lv_label_ins_text(Label_Text_Core, LV_LABEL_POS_LAST, "#0000ff M0内核系列:# SWM181 SWM190 SWM201 SWM260 \n");
	lv_label_ins_text(Label_Text_Core, LV_LABEL_POS_LAST, "#0000ff M4内核系列:# SWM320 SWM341 \n");
	lv_label_ins_text(Label_Text_Core, LV_LABEL_POS_LAST, "#daa520 TFT-LCD显示应用:# SWM19S SWM32S \n");
	lv_label_ins_text(Label_Text_Core, LV_LABEL_POS_LAST, "#daa520 SLCD显示应用:# SWM181 \n");
	lv_label_ins_text(Label_Text_Core, LV_LABEL_POS_LAST, "#20B2AA 电机应用:# SWM190 SWM201 \n");
	lv_label_ins_text(Label_Text_Core, LV_LABEL_POS_LAST, "#20B2AA 大家电应用:# SWM181 SWM260");

	lv_obj_align(Label_Text_Core, NULL, LV_ALIGN_CENTER, 0, 0); //设置相对位置
}

/**
 * @brief	删除顶部宣传标语
 * @param 	\
 * @retval	\
 */
static void pp_delete_text_top(void)
{
	if (Label_Text_Top != NULL)
	{
		LOG_INFO(" delete [Label_Text_Top]\r\n");
		lv_obj_del(Label_Text_Top);
		Label_Text_Top = NULL;
	}
}

/**
 * @brief	创建顶部宣传标语
 * @param 	parent 		- 页面父节点
 * @retval	\
 */
static void pp_create_text_top(lv_obj_t *parent)
{
	pp_delete_text_top();
	LOG_INFO(" create [Label_Text_Top]\r\n");

	//顶部标语
	Label_Text_Top = lv_label_create(parent, NULL);
	//设置中文字体样式
	label_set_font_style(Label_Text_Top, &Label_Style_Font);
	lv_label_set_text(Label_Text_Top, "#daa520 广东省# #3CB371 佛山市# #0000ff 南海区桂城街道# #ff0000 佛山华芯微特集成电路有限公司#");
	
	#if !GIF_SHOW_MODE
	lv_label_set_long_mode(Label_Text_Top, LV_LABEL_LONG_SROLL_CIRC);
	// LV_LABEL_LONG_SROLL_CIRC - 保持大小并循环滚动文本
	// LV_LABEL_LONG_SROLL 		- 保持大小并来回滚动文本
	
	#endif //GIF_SHOW_MODE
	
	lv_obj_set_size(Label_Text_Top,
					GET_X(2, 0, ICON_HB_W, 
	GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)) - GET_X(0, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)) - ICON_HB_W,
					ICON_HB_H * 9 / 10);

	lv_obj_align(Label_Text_Top, NULL, LV_ALIGN_IN_TOP_MID, 0,
				 (GET_X(2, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)) 
				- GET_X(0, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)) 
				- ICON_HB_W > lv_obj_get_width(Label_Text_Top))
					 ? ICON_HB_H : 0); //相对父级顶部居中对齐
}

/**
 * @brief	设置Label的字体样式-中文字库
 * @param 	\
 * @retval	\
 * @note	依赖于 Font_Var , 请确保其有效
 */
static void label_set_font_style(lv_obj_t *label, lv_style_t *style_label)
{
	lv_style_init(style_label);

	lv_style_set_text_font(style_label, LV_STATE_DEFAULT, &Font_Var); //设置字体
	lv_style_set_bg_opa(style_label, LV_STATE_DEFAULT, LV_OPA_COVER);
	lv_style_set_bg_color(style_label, LV_STATE_DEFAULT, LV_COLOR_WHITE); //白色背景

	lv_label_set_recolor(label, true); //允许重新着色

	lv_obj_add_style(label, LV_LABEL_PART_MAIN, style_label); //设置样式
}